import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.concurrent.ConcurrentHashMap;
import java.net.Socket;
import java.net.ServerSocket;
import java.io.SerializablePermission;
import java.io.Serializable;
import java.util.logging.Level;
import java.util.logging.Logger;


public class CentralServer{

    private transient ServerSocket ss;
    private ConcurrentHashMap<Integer, Address> storagePointer;
    private ConcurrentHashMap<Address,Integer> numberOfKeys;

    public CentralServer() throws Exception{
        storagePointer = new ConcurrentHashMap<Integer,Address>();
        numberOfKeys = new ConcurrentHashMap<Address,Integer>();
        ss = new ServerSocket(55555);
        ss.setReuseAddress(true);
        System.out.println("CentralServer up!");

        while(true){
            Socket client= ss.accept();
            System.out.println("Client connected");
            new Thread(new CentralServerT(client, storagePointer, numberOfKeys)).start();
        }



    }

    public void readStatus(){
 
            ObjectInputStream in;
        try {
            in = new ObjectInputStream(new FileInputStream("backup_storage.dat"));
            this.storagePointer = (ConcurrentHashMap<Integer, Address>) in.readObject();
            in.close();
            in = new ObjectInputStream(new FileInputStream("backup_keys.dat"));
            this.numberOfKeys = (ConcurrentHashMap<Address,Integer>) in.readObject();
            in.close();
        } catch (Exception ex) {
            Logger.getLogger(CentralServer.class.getName()).log(Level.SEVERE, null, ex);
        }
            
            
            

    }



    public static void main(String []args){

        try {
            CentralServer server = new CentralServer();
        } catch (Exception e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
    }

}




